<template>
  <div class="view-fill">
    <div
      v-if="!modifyInfo.visible && !assetDetailInfo.visible"
      class="app-container-flex"
    >
      <full-screen
        v-model="showDialog"
        title="摊销部门调整单明细"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="
              $permission('amortizationDeptAdjust:printOrder') &&
              infoList.data.status !== 1
            "
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!--          <el-button
                      v-if="
                        $permission('amortizationDeptAdjust:printOrder') &&
                        infoList.data.status !== 1
                      "
                      @click="print"
                      >打印</el-button
                    >-->
          <el-button
            v-if="
              $permission('amortizationDeptAdjust:download') &&
              infoList.data.status !== 1
            "
            @click="exportExcel"
            >导出excel</el-button
          >
          <el-button v-if="canUpdate" @click="update">修改</el-button>
          <el-button v-if="canReject" @click="reject">驳回</el-button>
          <el-button v-if="canRevoke" @click="revoke">撤回</el-button>
          <el-button v-if="canExamine" @click="examine">审核</el-button>
          <el-button v-if="canSubmit" @click="submit">提交</el-button>
          <el-button v-if="canCancel" @click="cancel">取消</el-button>
          <el-button v-if="canAdjust" @click="adjust">调整</el-button>
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList" />
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')" />
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :check-box="false"
                :tab-index="true"
                :field-list="tableInfo.fieldList"
              >
                <template v-slot:col-assetName="scope">
                  <span class="slip-edit" @click="showAssetDetail(scope.row)">{{
                    scope.row.assetName
                  }}</span>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </full-screen>
    </div>
    <order-modify
      v-if="modifyInfo.visible"
      :id="modifyInfo.id"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      @returnModify="returnModify"
    />
    <asset-card-detail
      v-if="assetDetailInfo.visible"
      :id="assetDetailInfo.id"
      v-model:visible="assetDetailInfo.visible"
      :title="assetDetailInfo.title"
    />
  </div>
</template>

<script setup name="amortizationDeptAdjustDetail">
import {
  adjustAmortizationDeptDetailCustomizeListAll,
  getAdjustAmortizationDeptById,
  submitAdjustAmortizationDept,
  checkAdjustAmortizationDept,
  rejectAdjustAmortizationDept,
  revokeAdjustAmortizationDept,
  excelAdjustAmortizationDept,
  printAdjustAmortizationDept,
  cancelAdjustAmortizationDept,
  adjustAdjustAmortizationDept,
} from '@/api/asset.js'
import { getCustomizeSetting } from '@/api/customize'
import { orderOpAffairAll } from '@/api/affair'
import { fatchTemplateByCode } from '@/api/base'
import OrderModify from './modify'
import AssetCardDetail from '@/views/asset/assetManage/assetCard/details'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'

const emits = defineEmits()
const { proxy } = getCurrentInstance()
const props = defineProps(['visible', 'tableRow'])

const infoList = ref({
  data: {},
  fieldList: [
    { label: '单号', value: 'orderNo', type: 'text' },
    { label: '状态', value: 'statusValue', type: 'text' },
    { label: '申请人', value: 'createUserName', type: 'text' },
    { label: '申请时间', value: 'createTime', type: 'text' },
    { label: '审核人', value: 'checkUserName', type: 'text' },
    { label: '审核时间', value: 'checkTime', type: 'text' },
    { label: '调整人', value: 'adjustUserName', type: 'text' },
    { label: '调整时间', value: 'adjustTime', type: 'text' },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.tableRow.id,
      },
    },
    queryParams: {},
  },
})
const modifyInfo = ref({
  id: '',
  visible: false,
  title: '',
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [],
})
const assetDetailInfo = ref({
  visible: false,
  id: '',
  title: '资产详情',
})
const affairTableInfo = ref({
  api: orderOpAffairAll,

  query: {
    orderType: 93,
    orderId: props.tableRow.id,
    sort: 'operate_time desc',
  },
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

const canUpdate = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:update') &&
    infoList.value.data.status === 1
  )
})
const canSubmit = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:submit') &&
    infoList.value.data.status === 1
  )
})
const canReject = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:reject') &&
    infoList.value.data.status === 2
  )
})
const canExamine = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:check') &&
    infoList.value.data.status === 2
  )
})
const canRevoke = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:revoke') &&
    (infoList.value.data.status === 2 || infoList.value.data.status === 3)
  )
})
const canCancel = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:cancel') &&
    infoList.value.data.status === 3
  )
})
const canAdjust = computed(() => {
  return (
    proxy.$permission('amortizationDeptAdjust:adjust') &&
    infoList.value.data.status === 3
  )
})
let obj = {
  adjustAfter: {
    label: '调整后值',
    value: 'adjustAfter',
    componentType: 'input',
    show: false,
    hidden: true,
  },
  adjustBefore: {
    label: '调整前值',
    value: 'adjustBefore',
    componentType: 'input',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()

function showAssetDetail(row) {
  assetDetailInfo.value.id = row.assetId
  assetDetailInfo.value.visible = true
}
//提交
function submit() {
  let ids = [infoList.value.data.id]
  submitAdjustAmortizationDept(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function reject() {
  //驳回
  let ids = [infoList.value.data.id]
  rejectAdjustAmortizationDept(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function examine() {
  //审核
  let ids = [infoList.value.data.id]
  checkAdjustAmortizationDept(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function revoke() {
  //撤回
  let ids = [infoList.value.data.id]
  revokeAdjustAmortizationDept(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
//取消
function cancel() {
  let ids = [infoList.value.data.id]
  cancelAdjustAmortizationDept(ids).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
//调整
function adjust() {
  adjustAdjustAmortizationDept(infoList.value.data.id).then((resp) => {
    if (resp && resp.success) {
      getMain()
      getList()
    }
  })
}
function returnModify() {
  if (modifyInfo.value.id) {
    getList()
  } else {
    search()
  }
}
function returnBack() {
  emits('returnBack', false)
}
function getMain() {
  getAdjustAmortizationDeptById(props.tableRow.id).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
    }
  })
}
function getList() {
  adjustAmortizationDeptDetailCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}

function update() {
  modifyInfo.value.id = infoList.value.data.id
  modifyInfo.value.title = '修改'
  modifyInfo.value.visible = true
}

const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'adjustAmortizationDeptDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            applicationId: infoList.value.data.id,
            orderId: infoList.value.data.id,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function printCallback() {}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      applicationId: infoList.value.data.id,
      orderId: infoList.value.data.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
//导出excel
function exportExcel() {
  excelAdjustAmortizationDept(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'assetAssetManageAmortizationDeptAdjustDetail-com.slip.spd.entity.vo.asset.AssetAdjustOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail,
        true
      )
      tableInfo.value.fieldList = obj.fieldList
      getMain()
      getList()
    }
  })
}
</script>

<style scoped></style>
